package persist

import (
	"GoSpider/carwler/engine"
	"context"
	"github.com/olivere/elastic"
	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
)

func ItemSaver(index string) (chan engine.Item,error) {

	client, err := elastic.NewClient(
		elastic.SetSniff(false),
		elastic.SetURL("http://192.168.1.180:9200/"),
	)
	if err != nil {
		return nil,err
	}

	out := make(chan engine.Item)
	go func() {
		itemCounter := 0
		for {
			item := <-out
			log.Printf("Item server: got item %d: %v", itemCounter, item)
			itemCounter++

			err := save(client,item,index)
			if err != nil {
				log.Warnf("Item Saver Error:saving item %v:%v", item, err)
			}

		}
	}()
	return out,nil
}

func save(client *elastic.Client,item engine.Item,index string) error {
	if item.Type == "" {
		return errors.New("must supply Type")
	}

	indexService := client.
		Index().
		Index(index).
		Type(item.Type)

	if item.Id != "" {
		indexService.Id(item.Id)
	}

	_, err := indexService.
			BodyJson(item).
			Do(context.Background())
	if err != nil {
		return err
	}

	return nil
}
